package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.CartService;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 购物车
 */

@RestController
// 开启跨域支持
@CrossOrigin
@RequestMapping("/cart")
public class CartController {

    // 先静态写死，后期动态获取
    // private static final String username = "ittest";

    @Autowired
    private CartService cartService;

    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 添加购物车
     * @param skuId sku商品id
     * @param num   商品数量
     * @return
     */
    @GetMapping("/addCart")
    public Result add(@RequestParam("skuId") String skuId, @RequestParam("num") Integer num) {

        String username = tokenDecode.getUserInfo().get("username");
        cartService.addCart(skuId, num, username);

        return new Result(true, StatusCode.OK, "加入购物车成功");
    }

    /**
     * 查询购物车列表数据
     * @return
     */
    @GetMapping("/list")
    public Map list(){
        String username = tokenDecode.getUserInfo().get("username");
        Map map = cartService.list(username);
        return map;
    }
}
